HBase 常用命令
进入HBase 命令行
./hbase shell
查看Hbase 的状态 status
hbase(main):007:0> status
1 active master, 0 backup masters, 1 servers, 0 dead, 8.0000 average load
显示HBase 中的表 list
list
---
hbase(main):008:0> list
TABLE
member
mytable
test
test0417
user
web_test
6 row(s) in 0.0100 seconds
=> ["member", "mytable", "test", "test0417", "user", "web_test"]
删除表 drop
删除表的前提是表要处于disable的状态,如果没有则disable 表
hbase(main):016:0> drop 'user'
0 row(s) in 3.1690 seconds
创建表 create
create 第一个参数是代表Row ,后面的代表Column
hbase(main):017:0> create 'user','info','data'
0 row(s) in 8.6390 seconds
=> Hbase::Table - user
hbase(main):018:0> desc 'user'
Table user is ENABLED
user
COLUMN FAMILIES DESCRIPTION
{NAME => 'data', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
2 row(s) in 0.0180 seconds
插入数据 put
需要主要的是在HBase中更新和创建记录数据是相同的命令,每一条记录将自动记录多次变更的情况,类似于SVN一样,但是在设计表的时候可以设置最大保存的version的最大数,读取的时候也可以读取指定的的版本数
直接给一个列插入数据(非列族下的列),类型为字符串和字典
hbase(main):020:0> put 'user','0001','info','only_info_value'
0 row(s) in 0.0700 seconds
#插入的是一个字典类型数据,注意字典需要双引号
hbase(main):022:0> put 'user','0002','info',"{'value':'1234'}"
0 row(s) in 0.0120 seconds
直接赋值给列族中的列,自动创建列,只添加一个的情况
hbase(main):025:0> put 'user','0003','data:age','18'
0 row(s) in 0.0150 seconds
hbase(main):026:0> scan 'user'
ROW COLUMN+CELL
0001 column=info:, timestamp=1492422232548, value=only_info_value
0002 column=info:, timestamp=1492422363530, value={'value':'1234'}
0003 column=data:age, timestamp=1492423135470, value=18
3 row(s) in 0.0200 seconds
添加多个列,实质上就是再执行一次put 操作,此时row_key和原来是一样的,如下所示:
hbase(main):027:0> put 'user','0003','data:gender','female'
0 row(s) in 0.0090 seconds
hbase(main):028:0> scan 'user'
ROW COLUMN+CELL
0001 column=info:, timestamp=1492422232548, value=only_info_value
0002 column=info:, timestamp=1492422363530, value={'value':'1234'}
0003 column=data:age, timestamp=1492423135470, value=18
0003 column=data:gender, timestamp=1492423353786, value=female
3 row(s) in 0.0110 seconds
读取数据get
读取某一行的所有数据
hbase(main):029:0> get 'user','0001'
COLUMN CELL
info: timestamp=1492422232548, value=only_info_value
1 row(s) in 0.0270 seconds
读取一行中一个列族的所有信息
(为了演示,给info列增加一个值)
hbase(main):033:0> put 'user','0001','info:123','ext_001'
0 row(s) in 0.0090 seconds
hbase(main):035:0> scan 'user'
ROW COLUMN+CELL
0001 column=info:, timestamp=1492422232548, value=only_info_value
0001 column=info:123, timestamp=1492496315262, value=ext_001
0002 column=info:, timestamp=1492422363530, value={'value':'1234'}
0003 column=data:age, timestamp=1492423135470, value=18
0003 column=data:gender, timestamp=1492423353786, value=female
3 row(s) in 0.0170 seconds
hbase(main):037:0> get 'user','0001','info'
COLUMN CELL
info: timestamp=1492422232548, value=only_info_value
info:123 timestamp=1492496315262, value=ext_001
2 row(s) in 0.0090 seconds
获取列族中某一个字段的值
hbase(main):038:0> get 'user','0003','data:age'
COLUMN CELL
data:age timestamp=1492423135470, value=18
1 row(s) in 0.0110 seconds
获取user表中row key为0003,列标示符中含有a的信息
有问题,待补充
读取全表数据scan
hbase(main):042:0> scan 'user'
ROW COLUMN+CELL
0001 column=info:, timestamp=1492422232548, value=only_info_value
0001 column=info:123, timestamp=1492496315262, value=ext_001
0002 column=info:, timestamp=1492422363530, value={'value':'1234'}
0003 column=data:age, timestamp=1492423135470, value=18
0003 column=data:gender, timestamp=1492423353786, value=female
3 row(s) in 0.0150 seconds
查询表中有多少行 count
hbase(main):043:0> count 'user'
3 row(s) in 0.0140 seconds
=> 3
默认是1000行显示一次,也可以调整
# 语法:count <table>, {INTERVAL => intervalNum, CACHE => cacheNum}
# INTERVAL设置多少行显示一次及对应的rowkey,默认1000;CACHE每次去取的缓存区大小,默认是10,调整该参数可提高查询速度
# 例如,查询表t1中的行数,每100条显示一次,缓存区为500
hbase(main)> count 't1', {INTERVAL => 100, CACHE => 500}
删除指定行某一列的数据 delete
hbase(main):049:0> get 'user','0001'
COLUMN CELL
info: timestamp=1492422232548, value=only_info_value
info:123 timestamp=1492496315262, value=ext_001
2 row(s) in 0.0090 seconds
hbase(main):050:0> delete 'user','0001','info:123'
0 row(s) in 0.0250 seconds
hbase(main):051:0> get 'user','0001'
COLUMN CELL
info: timestamp=1492422232548, value=only_info_value
1 row(s) in 0.0070 seconds
添加两个列族 alter table_name ,NAME =>列族名
hbase(main):052:0> desc 'user'
Table user is ENABLED
user
COLUMN FAMILIES DESCRIPTION
{NAME => 'data', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
2 row(s) in 0.0180 seconds
hbase(main):055:0> alter 'user',NAME =>'Content'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 3.2340 seconds
hbase(main):056:0> desc 'user'
Table user is ENABLED
user
COLUMN FAMILIES DESCRIPTION
{NAME => 'Content', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', COMPRESSION => 'NONE', VERSIONS => '1', TTL => 'FOR
EVER', MIN_VERSIONS => '0', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'data', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS =
> '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
3 row(s) in 0.0150 seconds
查询指定时间段的方法 TIMERANGE
hbase(main):012:0> scan 'user'
ROW COLUMN+CELL
0001 column=info:, timestamp=1492422232548, value=only_info_value
0002 column=info:, timestamp=1492422363530, value={'value':'1234'}
0003 column=data:age, timestamp=1492423135470, value=18
0003 column=data:gender, timestamp=1492423353786, value=female
3 row(s) in 0.0210 seconds
hbase(main):013:0> scan 'user',{TIMERANGE =>[1492422232548,1492422232549]}
ROW COLUMN+CELL
0001 column=info:, timestamp=1492422232548, value=only_info_value
1 row(s) in 0.0110 seconds